<template>
	<view class="viewcontent">
		<view class="viewcontenthead">
			<view class="fl" @click="$UTILS.goBack(1)"></view>
			<view class="fr" style="color: #fdf5fa;"> 物资归还</view>
			<view class="leftitem" ></view>
		</view>
		<view class="viewconten">
			 <view class="viewcontentitle">
				 <u-button @click="saoselecttask">扫描入库</u-button>
				 <u-button @click="selecttask">按任务入库</u-button>
			 </view>
			 <view class="viewcontenmes">
				 <view>
					<view class="contenmesbox" v-for="(item,index) in this.itemlist" :key="index" v-if="chec">
						<view class="contenmesboxtitle">
							<view>序号：{{index+1}}</view>
							<view>
								<image   src="../../../static/success-Green.svg" style="height:52rpx;width: 52rpx;" v-if="selectarr[index]" @click="checkedselectarr(index)"></image>
								<image   src="../../../static/success-Gray.svg" style="height:52rpx;width: 52rpx;" v-else @click="checkedselectarr(index)"></image>
							</view>
							</view>
						<view class="contenmesboxtitle"><view>物资名称：{{item.equipmentName}}</view></view>
						<view class="contenmesboxtitle"><view>物资编码：{{item.equipmentCode}}</view></view>
						<view class="contenmesboxtitle"><view>RFID：{{item.rfid}}</view></view>
					</view> 
				 </view>
			 </view>
		</view>
		<view class="viewcontentfooter">
			<u-button @click="sureinput">确认入库</u-button>
		</view>
		<keyboard-listener @keydown="onKeydown"></keyboard-listener>
		<keyboard-listener @keyup="onKeyup"></keyboard-listener>
		<myLoading v-if="loading" style="position: absolute;z-index: 9;"></myLoading>
	</view>
</template>

<script>
import keyboardListener from '../../../components/uniapp-keyboard-listener/src/keyboard-listener.vue'
	// #ifdef APP-PLUS
		// const pda = uni.requireNativePlugin("PDAModule");
		// const tone = uni.requireNativePlugin("ToneModule");
		// var log = uni.requireNativePlugin("LogModule")
	// #endif
let andoridModule = uni.requireNativePlugin('seuic-uhf');
export default {
	    components: {
			keyboardListener
			},
		onLoad() {
			
		},
		data() {
			return {
				itemlist:[],
				lastQueryTimestamp:0,
				rfidList:[],
				selectarr:[],
				loading:false,
				chec:true
			};
		},
		methods:{
			checkedselectarr(index){
				this.chec=false
				this.selectarr[index] = !this.selectarr[index]
				this.chec = true
			},
			selecttask(){
				let item = this.$UTILS.getitemlist().filter(item=>{return item.taskCode == this.$UTILS.gettaskCode() })
				this.itemlist =item[0].equipmentList
				this.itemlist.forEach(item =>{
					this.selectarr.push(false)
				})
			},
			async sureinput(){
				let equipmentInfos = []
				for(let i=0 ; i < this.itemlist.length ; i++){
					if(this.selectarr[i]){
						this.itemlist[i].equipmentStockStatus = 1
						equipmentInfos.push(this.itemlist[i])
					}
				}
				let equipmentInfoscopy = equipmentInfos
				if(JSON.stringify(equipmentInfoscopy) == '[]' ){  
					this.$UTILS.toast('无数据')
					return
				}
				this.loading = true
				let [error, res] = await  this.$UTILS.requestRest(this.$COMMON.REST_URL.PDATASK, 'PUT',JSON.stringify(equipmentInfos))
				if(res && res.statusCode == 200){
					if (res.data?.code == 401) {
						let jqdizhi = this.$UTILS.getunlinelogin()
						let ictdizhi = this.$UTILS.getunlineaddress()
						uni.clearStorageSync()
						this.$UTILS.setunlinelogin(jqdizhi);
						this.$UTILS.setunlineaddress(ictdizhi);
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/view-layout/login/login',
							});
						}, 500)
						
						this.$UTILS.toast('token过期，请从新登录')
						return
					}
					this.loading = false
					this.$UTILS.toast('提交成功')
				}else{
					this.loading = false
					this.$UTILS.toast('提交失败')
				}
			},
			saoselecttask(){
				this.itemlist = []
			},
			onKeydown(e) {
				if (e.keyCode == 123) {
					const now = Date.now();
					 // 节流间隔，1000 毫秒
					 const throttleDuration = 3000;
					 if (now - this.lastQueryTimestamp < throttleDuration) {
					     // 如果当前时间与最后一次执行的时间差小于节流间隔，则不执行
					     return;
					 }
					 this.lastQueryTimestamp = now;
						andoridModule.inventoryStart((e) => {
							if (e != '未寻到') {
								let jsonResult = JSON.parse(JSON.stringify(e));
								jsonResult.forEach(item =>{
									if (this.rfidList.indexOf(item.id) == -1) {
										 this.rfidList.push(item.id)
									}						 
								})
							} else {
								this.templedata = e
							}
						});
				}
			},
			onKeyup(e) {
					andoridModule.inventoryStop((e) => {
						this.templedata = e
					});
					this.loading = true
					this.pushitem()
					
			},
			 processData(result) {
				let jsonResult = JSON.parse(result);
				if (this.rfidList.indexOf(jsonResult.epc) == -1) {
					 this.rfidList.push(jsonResult.epc)
				}						
			},
			async pushitem(){
				let rfids = this.rfidList.join(',')
				console.log(rfids)
				let [error, res] = await  this.$UTILS.requestRest(this.$COMMON.REST_URL.SUPPLIESRFIDS+`/${rfids}`, 'GET')
				if (res.data?.code == 401) {
					let jqdizhi = this.$UTILS.getunlinelogin()
					let ictdizhi = this.$UTILS.getunlineaddress()
					uni.clearStorageSync()
					this.$UTILS.setunlinelogin(jqdizhi);
					this.$UTILS.setunlineaddress(ictdizhi);
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/view-layout/login/login',
						});
					}, 500)
					
					this.$UTILS.toast('token过期，请从新登录')
					return
				}
				if(res.data.result){
					res.data.result.forEach(item=>{
						let flag = false
						this.itemlist =  this.itemlist.map((ite,index)=>{
							if(item.equipmentRfid == ite.equipmentRfid){
								flag = true
								this.selectarr[index] = true
								return ite
							}
							return ite
						})
						if(flag){
							return
						}else{
							this.selectarr.push(true)
							this.itemlist.push(item)
						}
						
					})
				}
				
				this.loading = false
			}
		}
	}
</script>
<style lang="scss">
 .viewcontent{
	 display: flex;
	 flex-direction: column;
	 width: 100%;  
	 height: 100vh; 
	 box-sizing: border-box;
	 padding-top: 55rpx;
	   .viewcontenthead{
	   		 width: 100%;
	   		 height: 5%;
	   		 display: flex;
	   		 justify-content: space-between;
	   		 align-items: center;
	   		 .fl{
	   			 width: 100rpx;
	   			 height: 10vh;
	   			 line-height: 10vh;
	   			 background: url("/static/right-arrow.png") no-repeat center;
	   			 background-size: 14%;
	   			 transform: rotate(180deg);
	   			 cursor: pointer; // 增加鼠标指针样式
	   		 }
	   		 .fr{
	   			 cursor: pointer; // 增加鼠标指针样式
	   		 }
	   		 .leftitem{
	   			 width: 100rpx;
	   			 display: flex;
	   			 justify-content: center;
	   			 z-index: 999;
	   		 }
	   } 
	   .viewconten{
		   width: 100%;
		   height: 88%;
		   min-height: 88%;
		   box-sizing: border-box;
		   margin-top: 15rpx;
		   .viewcontentitle{
			    height: 200rpx;
				width: 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				button{
					width: 80%;
				}
		     }
			 .viewcontenmes{
				 width: 98%;
				 height: calc(100% - 260rpx);
				 min-height:calc(100% - 260rpx) ;
				 margin: 30rpx  auto 0;
				 border: 1px solid #FFFFFF;
				 border-radius: 15px;
				 background-color:rgba(47, 40, 83, 0.8) ;
				 font-family:PingFang SC;
				 font-weight:550;
				 color:rgba(250, 249, 249, 0.8);
				 font-size:16px;
				 overflow: auto;
				 box-sizing: border-box;
				 padding: 24rpx;
				 .contenmesbox{
					 border: 1px solid #FFFFFF;
					 border-radius: 15px;
					 box-sizing: border-box;
					 margin: 10rpx  auto ;
					 width: 100%;
					 height: 200rpx;
					 .contenmesboxtitle{
					 						 width: 100%;
					 						 height: 25%;
					 						 box-sizing: border-box;
					 						 padding-left: 15rpx;
					 						 display: flex;
					 						 justify-content: space-between;
					 						 box-sizing: border-box;
					 						 padding-right: 15rpx;
					 						 view{
					 							 text-overflow: ellipsis;
					 							 overflow: hidden;
					 							 white-space: nowrap;
					 						 }
					 }
				 }
				 .contenmesbox:last-child{
					 margin: 0;
				 }
			 }
		   }
		 .viewcontentfooter{
		 	width: 100%;
		 	height: 5%;
		 	button{
		 	width: 80%;
			height: 100%;
		 	}
		 }  
	}
</style>